﻿using B.S.BaseData.Api.Read.Command.GoodsCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;
using System.Collections.Generic;

namespace B.S.BaseData.Api.Read.CommandHandler.GoodsCommandHandler
{
    public class GoodTypeShowCommandHandler : IRequestHandler<GoodTypeShowCommand, APIResult<List<GoodsType>>>
    {
        private readonly IBaseRepository<GoodsType> _repository;
        public GoodTypeShowCommandHandler(IBaseRepository<GoodsType> repository)
        {
            _repository = repository;
        }

        public Task<APIResult<List<GoodsType>>> Handle(GoodTypeShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<GoodsType>> result=new APIResult<List<GoodsType>>();
            try
            {
                var list = _repository.GetAll().Where(x => x.IsDeleted == false);
                result.Code = ApiEnum.Success;
                result.Msg = "商品名称下拉框显示成功";
                result.Data=list.ToList();
                return Task.FromResult(result);
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.Success;
                result.Msg = $"商品名称下拉框显示异常,异常为:{ex.Message}";
                return Task.FromResult(result);
            }
        }
    }
}
